<?php

namespace App\Http\Controllers;

use App\Eloquent\Ad;
use App\Eloquent\Article;
use App\Eloquent\ArticleCategory;
use Illuminate\Http\Request;


/**
 * 文章控制器展示
 *
 * Class ArticleController
 * @package App\Http\Controllers
 */
class ArticleController extends Controller
{
    /**
     * 文章ID
     */
    const ARTICLE_TYPE_ARTICLE = 4;
    /**
     * 平台公告ID
     */
    const ARTICLE_TYPE_NOTICE = 1;
    /**
     * 帮助中心ID
     */
    const ARTICLE_TYPE_HELP = 2;
    /**
     * 关于我们ID
     */
    const ARTICLE_TYPE_ABOUT = 3;
    /**
     * 新手注册文章id
     */
    const REGISTER = 39;


    /**
     * 查询帮助中心
     *
     * @param int $article_category_id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function help($article_category_id = 0)
    {

        //查询的分类名称
        $article_category_name = null;

        //查询帮助中心下所有的文章分类
        $article_categories = ArticleCategory::get_child_categories(self::ARTICLE_TYPE_HELP);

        //展示的文章内容
        $articles = null;

        if ($article_category_id == 0) {
            //默认查询帮助中心下第一个子分类
            $article_category_name = $article_categories[0]->name;
            $articles = $article_categories[0]->get_articles_paginate(8);

        } else {
            //判断查询的分类ID是否是 帮助中心的 子级文章分类
            if (!ArticleCategory::is_child_category(self::ARTICLE_TYPE_HELP, $article_category_id)) {
                abort('404');
            }

            foreach ($article_categories as $article_category) {
                if ($article_category->id == $article_category_id) {
                    $article_category_name = $article_category->name;
                    $articles = $article_category->get_articles_paginate(8);
                }
            }
        }

        $data = [
            'article_category_name' => $article_category_name,
            'article_categories' => $article_categories,
            'articles' => $articles,
        ];

        return view('web.articles.help')->with($data);
    }

    /**
     * 查询帮助中心下某篇文章详情
     *
     * @param int $article_id
     * @return $this
     */
    public function help_article_info(Request $request,$article_id)
    {
        $preview = $request->input('preview');
        if(isset($preview) && !empty($preview) && ($preview == 1)){
            //预览
            $article = Article::findOrFail($article_id);
        }else{
            //非预览
            $article = Article::where('id', $article_id)
                ->where('display', 1)
                ->where('published_at', '<=', date('Y-m-d H:i:s'))
                ->first();
            if (empty($article)) {
                return abort(404);
            }
        }

        //查询该文章是否属于帮助中心
        if (!ArticleCategory::is_child_category(self::ARTICLE_TYPE_HELP, $article->article_category_id)) {
            abort(404);
        }

        //查询帮助中心下所有的文章分类
        $article_categories = ArticleCategory::get_child_categories(self::ARTICLE_TYPE_HELP);

        //查询该文章对应的分类名
        $article_category_name = null;
        foreach ($article_categories as $article_category) {
            if ($article_category->id == $article->article_category_id) {
                $article_category_name = $article_category->name;
            }
        }

        $data = [
            'article' => $article,
            'article_categories' => $article_categories,
            'article_category_name' => $article_category_name,
        ];

        return view('web.articles.help_article_info')->with($data);
    }

    /**
     * 查询平台公告
     *
     * @param int $article_category_id
     * @return $this
     */
    public function notice($article_category_id = 0)
    {

        //查询的分类名称
        $article_category_name = null;

        //查询平台公告下所有的文章分类
        $article_categories = ArticleCategory::get_child_categories(self::ARTICLE_TYPE_NOTICE);

        //展示的文章内容
        $articles = null;

        if ($article_category_id == 0) {
            //默认查询平台公告下第一个子分类
            $article_category_name = $article_categories[0]->name;
            $articles = $article_categories[0]->get_articles_paginate(8);

        } else {
            //判断查询的分类ID是否是 平台公告的 子级文章分类
            if (!ArticleCategory::is_child_category(self::ARTICLE_TYPE_NOTICE, $article_category_id)) {
                abort('404');
            }

            foreach ($article_categories as $article_category) {
                if ($article_category->id == $article_category_id) {
                    $article_category_name = $article_category->name;
                    $articles = $article_category->get_articles_paginate(8);
                }
            }
        }

        $data = [
            'article_category_name' => $article_category_name,
            'article_categories' => $article_categories,
            'articles' => $articles,
        ];

        return view('web.articles.notice')->with($data);
    }

    /**
     * 查询平台公告下某篇文章详情
     *
     * @param int $article_id
     * @return $this
     */
    public function notice_article_info(Request $request,$article_id)
    {
        $preview = $request->input('preview');
        if(isset($preview) && !empty($preview) && ($preview == 1)){
            //预览
            $article = Article::findOrFail($article_id);
        }else{
            //非预览
            $article = Article::where('id', $article_id)
                ->where('display', 1)
                ->where('published_at', '<=', date('Y-m-d H:i:s'))
                ->first();
            if (empty($article)) {
                return abort(404);
            }
        }

        //查询该文章是否属于帮助中心
        if (!ArticleCategory::is_child_category(self::ARTICLE_TYPE_NOTICE, $article->article_category_id)) {
            abort(404);
        }

        //查询帮助中心下所有的文章分类
        $article_categories = ArticleCategory::get_child_categories(self::ARTICLE_TYPE_NOTICE);

        //查询该文章对应的分类名
        $article_category_name = null;
        foreach ($article_categories as $article_category) {
            if ($article_category->id == $article->article_category_id) {
                $article_category_name = $article_category->name;
            }
        }

        $data = [
            'article' => $article,
            'article_categories' => $article_categories,
            'article_category_name' => $article_category_name,
        ];

        return view('web.articles.notice_article_info')->with($data);
    }

    /**
     * 查询所有帮助中心文章分类($id查询指定分类下的文章)
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function article_help_list($id = 0)
    {

        $article_type = $id != 0 ? ArticleCategory::findOrFail($id) : null;

        //查询帮助中心所有的下级子文章分类,帮助中心的分类id为1,
        $article_categories = ArticleCategory::where('parent', self::ARTICLE_TYPE_HELP)->orderBy('sort', 'desc')->get();


        if (count($article_categories) == 0) {
            abort(404);
        }

        $articles = null;
        $article_name = null;

        if ($id == 0) {
            //取得第一个分类名,如果不为null
            $article_name = $article_categories[0]->name;

            //默认查询第一个分类下的所有文章列表(标题和描述|分页)
            $articles = Article::select('id', 'title', 'description')
                ->where('article_category_id', $article_categories[0]->id)
                ->where('display', 1)
                ->where('published_at', '<=', date('Y-m-d H:i:s'))
                ->orderBy('sort', 'desc')
                ->orderBy('published_at', 'desc')
                ->paginate(8);
        } else {
            //查找要$id对应的分类名称
            $article_name = $article_type->name;

            //查询$id对应文章分类下的文章
            $articles = Article::select('id', 'title', 'description')
                ->where('article_category_id', $id)
                ->where('display', 1)
                ->where('published_at', '<=', date('Y-m-d H:i:s'))
                ->orderBy('sort', 'desc')
                ->orderBy('published_at', 'desc')
                ->paginate(8);
        }

        $data = [
            'article_categories' => $article_categories,
            'articles' => $articles,
            'article_name' => $article_name
        ];

        return view("web.articles.help_list", $data);
    }

//    /**
//     * 查看帮助中心下子分类中某一篇文章详情(根据id查询)
//     *
//     * @param int $id
//     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
//     */
//    public function article_help_info(int $id)
//    {
//        //查询该文章
//        $article = Article::findOrFail($id);
//
//
//        //查询帮助中心所有的下级子文章分类,帮助中心的分类id为1,
//        $article_categories = ArticleCategory::where('parent', '1')->orderBy('sort', 'desc')->get();
//
//        $article_name = null;
//        //找到该文章对应的文章分类名称
//        foreach ($article_categories as $article_category) {
//            if ($article_category->id == $article->article_category_id) {
//                $article_name = $article_category->name;
//                break;
//            }
//        }
//
//        $data = [
//            'article' => $article,
//            'article_name' => $article_name,
//            'article_categories' => $article_categories
//        ];
//
//        return view('web.articles.help_info', $data);
//    }

    /**
     * 查询平台公告下指定的文章分类
     *
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function article_notice_list($id = 0)
    {

        $article_type = $id != 0 ? ArticleCategory::findOrFail($id) : null;

        //查询帮助中心所有的下级子文章分类,帮助中心的分类id为1,
        $article_categories = ArticleCategory::where('parent', '8')->orderBy('sort', 'desc')->get();


        if (count($article_categories) == 0) {
            abort(404);
        }

        $articles = null;
        $article_name = null;

        if ($id == 0) {
            //取得第一个分类名,如果不为null
            $article_name = $article_categories[0]->name;

            //默认查询第一个分类下的所有文章列表(标题和发布时间|分页)
            $articles = Article::select('id', 'title', 'published_at')
                ->where('article_category_id', $article_categories[0]->id)
                ->where('display', 1)
                ->where('published_at', '<=', date('Y-m-d H:i:s'))
                ->orderBy('sort', 'desc')
                ->orderBy('published_at', 'desc')
                ->paginate(8);
        } else {
            //查找要$id对应的分类名称
            $article_name = $article_type->name;

            //查询$id对应文章分类下的文章
            $articles = Article::select('id', 'title', 'published_at')
                ->where('article_category_id', $id)
                ->where('display', 1)
                ->where('published_at', '<=', date('Y-m-d H:i:s'))
                ->orderBy('sort', 'desc')
                ->orderBy('published_at', 'desc')
                ->paginate(8);
        }

        $data = [
            'article_categories' => $article_categories,
            'articles' => $articles,
            'article_name' => $article_name
        ];

        return view('web.articles.notice_list', $data);
    }

    /**
     * 根据id展示平台公告下某一篇文章详情
     *
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function article_notice_info(Request $request,$id)
    {
        $preview = $request->input('preview');
        if(isset($preview) && !empty($preview) && ($preview == 1)){
            //预览
            $article = Article::findOrFail($id);
        }else{
            //非预览
            $article = Article::where('id', $id)
                ->where('display', 1)
                ->where('published_at', '<=', date('Y-m-d H:i:s'))
                ->first();
            if (empty($article)) {
                return abort(404);
            }
        }


        //查询平台公告下级子文章分类,平台公告的分类id为8,
        $article_categories = ArticleCategory::where('parent', '8')->orderBy('sort', 'desc')->get();

        $article_name = null;
        //找到该文章对应的文章分类名称
        foreach ($article_categories as $article_category) {
            if ($article_category->id == $article->article_category_id) {
                $article_name = $article_category->name;
                break;
            }
        }

        $data = [
            'article' => $article,
            'article_name' => $article_name,
            'article_categories' => $article_categories
        ];

        return view('web.articles.notice_info', $data);
    }

    /**
     * 文章汇总
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        //查询所有的平台公告下级推荐文章分类(平台公告ID为8)
        $notice_article_categories = ArticleCategory::get_child_categories(self::ARTICLE_TYPE_NOTICE);


        //查询帮助中心所有的下级子文章分类,帮助中心的分类id为1,
        $help_article_categories = ArticleCategory::get_child_categories(self::ARTICLE_TYPE_HELP);

        //查询新手注册引导文章
        $xszc = Article::where('article_category_id', self::REGISTER)
            ->where('display', 1)
            ->where('published_at', '<=', date('Y-m-d H:i:s'))
            ->orderBy('sort', 'desc')
            ->orderBy('created_at', 'desc')
            ->limit(8)->get();

        // dd($xszc);

        //查询关于我们的所有下级推荐文章(关于我们的Id为14)
        $about_articles = Article::where('article_category_id', self::ARTICLE_TYPE_ABOUT)
            ->where('display', 1)
            ->where('published_at', '<=', date('Y-m-d H:i:s'))
            ->where('recommended', 1)
            ->orderBy('sort', 'desc')
            ->orderBy('created_at', 'desc')
            ->get();


        //查询文章分类下推荐的2个板块
        $article_recommend_categories = ArticleCategory::where('parent', self::ARTICLE_TYPE_ARTICLE)
            ->where('recommended', '1')
            ->orderBy('sort', 'desc')
            ->limit(2)->get();


        //返回的数据
        $data = [
            'notice_article_categories' => $notice_article_categories,
            'help_article_categories' => $help_article_categories,
            'about_articles' => $about_articles,
            'article_recommend_categories' => $article_recommend_categories,
            'xszc'=>$xszc
        ];

        return view("web.articles.index", $data);
    }

    /**
     * 关于我们文章展示
     *
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function about(Request $request,$id = 0)
    {

        $article = null;

        //查询关于我们下所有的文章
        $articles = Article::where('article_category_id', self::ARTICLE_TYPE_ABOUT)
            ->where('display', 1)
            ->where('published_at', '<=', date('Y-m-d H:i:s'))
            ->orderBy('sort', 'desc')
            ->orderBy('published_at', 'desc')
            ->get();


        if ($id == 0) {
            //默认查询第一篇文章
            $query_article = $articles[0];
        } else {

            $preview = $request->input('preview');
            if(isset($preview) && !empty($preview) && ($preview == 1)){
                //预览
                $query_article = Article::findOrFail($id);
            }else{
                //非预览
                $query_article = Article::where('id', $id)
                    ->where('display', 1)
                    ->where('published_at', '<=', date('Y-m-d H:i:s'))
                    ->first();
                if (empty($query_article)) {
                    return abort(404);
                }
            }

            //查询该指定的文章内容
//            $query_article = Article::findOrFail($id);
            //查询该文章是否属于关于我们
            if (!ArticleCategory::is_child_article(self::ARTICLE_TYPE_ABOUT, $query_article->id)) {
                abort(404);
            }
        }

        $data = [
            'query_article' => $query_article,
            'articles' => $articles,
        ];

        return view("web.articles.about", $data);
    }

    /**
     * 新手注册文章展示
     *
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function xszc(Request $request,$id = 0)
    {

        $article = null;

        //新手注册下所有的文章
        $articles = Article::where('article_category_id', self::REGISTER)
            ->where('display', 1)
            ->where('published_at', '<=', date('Y-m-d H:i:s'))
            ->orderBy('sort', 'desc')
            ->orderBy('published_at', 'desc')
            ->get();


        if ($id == 0) {
            //默认查询第一篇文章
            $query_article = $articles[0];
        } else {

            $preview = $request->input('preview');
            if(isset($preview) && !empty($preview) && ($preview == 1)){
                //预览
                $query_article = Article::findOrFail($id);
            }else{
                //非预览
                $query_article = Article::where('id', $id)
                    ->where('display', 1)
                    ->where('published_at', '<=', date('Y-m-d H:i:s'))
                    ->first();
                if (empty($query_article)) {
                    return abort(404);
                }
            }

            //查询该指定的文章内容
//            $query_article = Article::findOrFail($id);
            //查询该文章是否属于新手注册
            if (!ArticleCategory::is_child_article(self::REGISTER, $query_article->id)) {
                abort(404);
            }
        }

        $data = [
            'query_article' => $query_article,
            'articles' => $articles,
        ];

        return view("web.articles.xszc", $data);
    }

    public function list($article_category = 0)
    {
        //要查询的文章分类
        $query_article_category = null;

        //查询文章下所有的子级文章分类
        $article_child_categories = ArticleCategory::get_child_categories(self::ARTICLE_TYPE_ARTICLE);

        if ($article_category == 0) {
            //默认查询第一个文章分类
            $query_article_category = $article_child_categories[0];
        } else {
            //查询该文章分类是否属于文章的子级文章分类
            if (!ArticleCategory::is_child_category(self::ARTICLE_TYPE_ARTICLE, $article_category)) {
                abort(404);
            }

            //查询指定的文章分类信息
            foreach ($article_child_categories as $article_child_category) {
                if ($article_child_category->id == $article_category) {
                    $query_article_category = $article_child_category;
                }
            }
        }

        //占位广告查询
        $ad1 = AD::where('position', 'article_list')->orderBy('sort', 'desc')->limit(1)->get();
        $ad2 = AD::where('position', 'article_list')->orderBy('sort', 'desc')->limit(1)->skip(1)->get();

        //查询分类下的文章
        $articles = $query_article_category->get_articles_paginate(10);

        $data = [
            'query_article_category' => $query_article_category,
            'article_child_categories' => $article_child_categories,
            'articles' => $articles,
            'ad1' => $ad1,
            'ad2' => $ad2,
        ];


        return view('web.articles.list')->with($data);
    }

    /**
     * 展示文章分类下子级文章分类下 指定文章详情
     *
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function article_information_info(Request $request,$id)
    {

        $preview = $request->input('preview');
        if(isset($preview) && !empty($preview) && ($preview == 1)){
            //预览
            $article = Article::findOrFail($id);
        }else{
            //非预览
            $article = Article::where('id', $id)
                ->where('display', 1)
                ->where('published_at', '<=', date('Y-m-d H:i:s'))
                ->first();
            if (empty($article)) {
                return abort(404);
            }
        }

//        //查询要展示的文章
//        $article = Article::where('id', $id)
//            ->where('display', 1)
//            ->where('published_at', '<=', date('Y-m-d H:i:s'))
//            ->first();

        if (empty($article)) {
            abort(404);
        }

        //获取当前文章所属分类
        $article_type = ArticleCategory::findOrFail($article->article_category_id);

        //判断当前分类是否属于文章分类
        if (!ArticleCategory::where('id', $article_type->id)->where('parent', self::ARTICLE_TYPE_ARTICLE)->exists()) {
            //不属于当前分类下应该出现的文章
            abort(404);
        }

        //判断文章为(页面或文章)
        if ($article->article_type == 1) {
            //返回页面
            return view('web.articles.information_info2', ['article' => $article]);
        }

        //查询下一篇文章 ......
        //查询上一篇文章 ......

        //占位广告查询
        $ad1 = Ad::where('position', 'article_list')->orderBy('sort', 'desc')->limit(1)->get();
        $ad2 = Ad::where('position', 'article_list')->orderBy('sort', 'desc')->limit(1)->skip(1)->get();

        //查询当前文章分类下的推荐文章(查询8条)
        $recommends = Article::where('article_category_id', $article_type->id)
            ->where('recommended', 1)
            ->where('display', 1)
            ->where('published_at', '<=', date('Y-m-d H:i:s'))
            ->orderBy('sort', 'desc')
            ->orderBy('published_at', 'desc')
            ->limit(8)->get();

        return view('web.articles.information_info', ['article' => $article, 'recommends' => $recommends, 'ad1' => $ad1, 'ad2' => $ad2]);
    }

    /**
     * 根据标签查询该标签下所有的文章
     *
     * @param string $tag
     */
    public function article_query(int $article_type_id, string $tag)
    {

        //查询文章下所有的子级文章分类(文章id为16)
        $article_types = ArticleCategory::where('parent', self::ARTICLE_TYPE_ARTICLE)
            ->orderBy('recommended', 'desc')
            ->orderBy('sort', 'desc')
            ->get();

        //查询包含该标签的所有文章,在当前分类下
        $articles = Article::where('tags', "like", '%' . $tag . '%')
            ->where('article_category_id', $article_type_id)
            ->where('display', 1)
            ->where('published_at', '<=', date('Y-m-d H:i:s'))
            ->orderBy('sort', 'desc')
            ->orderBy('published_at', 'desc')
            ->paginate(6);

        //查询该分类下面所有推荐的文章
        $recommends = Article::where('tags', "like", '%' . $tag . '%')
            ->where('recommended', 1)
            ->where('display', 1)
            ->where('published_at', '<=', date('Y-m-d H:i:s'))
            ->orderBy('published_at', 'desc')
            ->orderBy('sort', 'desc')
            ->limit(8)
            ->get();

        //占位广告查询
        $ad1 = Ad::where('position', 'article_list')->limit(1)->get();
        $ad2 = Ad::where('position', 'article_list')->limit(1)->skip(1)->get();


        $data = [
            'articles' => $articles,
            'recommends' => $recommends,
            'ad1' => $ad1,
            'ad2' => $ad2,
            'article_types' => $article_types,
            'article_type_id' => $article_type_id
        ];

        return view('web.articles.article_query', $data);
    }

    /**
     * 新手指导
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function article_guide(){
        return view('web.articles.article_guide');
    }
    /**
     * 手机宣传
     */
    public function information_phone(){
        return view('web.articles.information_phone');
    }
    /**
     * 易极付操作指南
     */
    public function yijifu(){
        return view('web.articles.yijifu');
    }
    /**
     * 安全保障页面
     */
    public function SecurityGuarantee(){
        return view('web.articles.Security_guarantee');
    }
}